//
//  SDGroupCell.m
//  SDNestedTablesExample
//
//  Created by Daniele De Matteis on 21/05/2012.
//  Copyright (c) 2012 Daniele De Matteis. All rights reserved.
//

#import "SDGroupCell.h"
#import <QuartzCore/QuartzCore.h>
#import "SDNestedTableViewController.h"
#import "clsOtherFun.h"
#import "clsFileFun.h"

@implementation SDGroupCell


@synthesize parentTable;
@synthesize Allview;
@synthesize lbWaybill;
//@synthesize lbStatus;
@synthesize lbRecei;
@synthesize lbDate;
@synthesize lbFreight;
@synthesize subTable;

@synthesize isExpanded;
@synthesize subCellsAmt;
@synthesize cellIndex;

@synthesize selectedIconImage;
@synthesize unSelectedIconImage;


+ (int) getHeight
{
    return height;
}

+ (int) getsubCellHeight1
{
    return subCellHeight1;
}
+ (int) getsubCellHeight2
{
    return subCellHeight2;
}
- (void)awakeFromNib
{
    
}

- (void)FillData:(clsDifficult *)pcls
{
    if (!self.lbWaybill) {
        self.lbWaybill = [self creatLabel:301];
        [Allview addSubview:self.lbWaybill];
    }
    if (!self.lbRecei) {
        self.lbRecei = [self creatLabel:302];
        self.lbRecei.adjustsFontSizeToFitWidth=YES;
        [Allview addSubview:self.lbRecei];
    }
//    if (!self.lbDate) {
//        self.lbDate = [self creatLabel:303];
//        [Allview addSubview:self.lbDate];
//    }
    if (!self.lbFreight) {
        self.lbFreight = [self creatLabel:304];
        [Allview addSubview:self.lbFreight];
        [clsOtherFun addAllLayoutConstraints:Allview];
    }
    subCellHeight = subCellHeight2;
    self.lbWaybill.text=pcls.Oid;
//    self.lbStatus.text=pcls.Status;
    self.lbRecei.text=pcls.Date;
//    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
//    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate* date = [formater dateFromString:pcls.Date];
//    date =  [NSDate dateWithTimeInterval:3*24*60*60 sinceDate:date];
//    self.lbDate.text=[formater stringFromDate:date];
    
    
    self.lbFreight.text=[NSString stringWithFormat:@"%.2f元",[pcls.DaoFu floatValue]+[pcls.Price floatValue]];
}
- (UILabel *)creatLabel:(NSInteger)tag
{
    UILabel *label = [[UILabel alloc]init];
    label.tag=tag;
    label.font=[UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor=[UIColor blackColor];
    return label;
}
- (void)FillDataWithCate:(clsCategory *)pcls
{
    
    if (!self.lbWaybill) {
        self.lbWaybill = [self creatLabel:301];
        [Allview addSubview:self.lbWaybill];
    }
    if (!self.lbStatus) {
        self.lbStatus = [self creatLabel:302];
        [Allview addSubview:self.lbStatus];
    }
    if (!self.lbRecei) {
        self.lbRecei = [self creatLabel:303];
        self.lbRecei.adjustsFontSizeToFitWidth=YES;
        [Allview addSubview:self.lbRecei];
    }
    if (!self.lbDate) {
        self.lbDate = [self creatLabel:304];
        self.lbDate.adjustsFontSizeToFitWidth=YES;
        [Allview addSubview:self.lbDate];
    }
    if (!self.lbFreight) {
        self.lbFreight = [self creatLabel:305];
        [Allview addSubview:self.lbFreight];
        [clsOtherFun addAllLayoutConstraints:Allview];
    }
    subCellHeight = subCellHeight1;
    self.lbWaybill.text=pcls.ExpressID;
    self.lbStatus.text=pcls.DType;
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate* date = [formater dateFromString:pcls.Ddate];
    NSDateFormatter* formater1 = [[NSDateFormatter alloc] init];
    [formater1 setDateFormat:@"yyyy.MM.dd"];
    NSString *recei = [formater1 stringFromDate:date];
    self.lbRecei.text=recei;
    [formater setDateFormat:@"yyyy-MM-dd"];
    date =  [formater dateFromString:pcls.dqDate];
    self.lbDate.text=[formater1 stringFromDate:date];
    
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd"];
    NSDate *fromdate=[format dateFromString:pcls.dqDate];
    NSInteger day=0;
    if (fromdate) {
        NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
        NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
        NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
        NSLog(@"fromdate=%@",fromDate);
        //获取当前时间
        NSDate *date1 = [NSDate date];
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date1];
        NSDate *localeDate = [date1  dateByAddingTimeInterval: interval];
        NSLog(@"enddate=%@",localeDate);
        
        double intervalTime = [localeDate timeIntervalSinceReferenceDate] - [fromDate timeIntervalSinceReferenceDate];
        
        long lTime = (long)intervalTime;
        day = lTime/60/60/24;
    }
    
   
    float money = [clsOtherFun MathMoney:[pcls.Long floatValue] :[pcls.Width floatValue] :[pcls.Height floatValue] :day];
    self.lbFreight.text=[NSString stringWithFormat:@"%.0f元",[pcls.daofu floatValue]+[pcls.Price floatValue]+money];
    if ([pcls.IsBig integerValue]==1) {
        self.Allview.backgroundColor = [UIColor redColor];
        for (UIView *view in self.Allview.subviews) {
            if ([view isKindOfClass:[UILabel class]]) {
                UILabel *label = (UILabel *)view;
                label.textColor = [UIColor whiteColor];
            }
        }
    }else{
        self.Allview.backgroundColor = [UIColor whiteColor];
        for (UIView *view in self.Allview.subviews) {
            if ([view isKindOfClass:[UILabel class]]) {
                UILabel *label = (UILabel *)view;
                label.textColor = [UIColor blackColor];
            }
        }
    }
}

#pragma mark - Table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return subCellsAmt;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDSubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SDSubCell" owner:self options:nil] lastObject];
    }
    
    cell = [self.parentTable item:self setSubItem:cell forRowAtIndexPath:indexPath];
    
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return subCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SDSubCell *cell = (SDSubCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil)
    {
        cell = (SDSubCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    }
    
    cell.cellIndex = indexPath.row;
    [self.parentTable groupCell:self didSelectSubCell:cell withIndexPath:indexPath andWithTap:YES];
}

@end
